package com.hds.modules.sys.controller.base;

import com.hds.common.annotation.SysLog;
import com.hds.common.exception.RRException;
import com.hds.common.utils.DateUtils;
import com.hds.common.utils.R;
import com.hds.modules.sys.entity.config.UploadFileConfig;
import com.hds.modules.sys.utils.DownloadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 下载文件
 *
 * @Author guonan
 * @Email guonan@yewcn.com
 * @Description
 * @Date 2018/6/12 14:16
 */
@RestController
@RequestMapping("/sys/download")
public class SysDownloadController {

    @Autowired
    UploadFileConfig uploadFileConfig;


    @SysLog("文件下载")
    @GetMapping(value="/file/{name:.+}")
    public R download(HttpServletResponse response, @PathVariable("name") String name){
        try{
            DownloadUtil.download(response, name, DateUtils.format(new Date(), "yyyyMMddHHmmss"),
                    uploadFileConfig.getActivateExcelPath());
        }catch(RRException e){
            return R.error("下载文件失败");
        }
        return R.ok("下载完成");
    }

    @SysLog("读取文件列表")
    @GetMapping(value="/file/list")
    public R readFileList(){

        try {
            List<String> list = DownloadUtil.readfile(uploadFileConfig.getActivateExcelPath());
            return R.ok().put("list", list);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.error();
    }

}
